﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StripeOne.ShortenUrl.Model;
using StripeOne.ShortenUrl.Database;
using System.Data.Objects;
using StripeOne.ShortenUrl.Model.Converters;
using StripeOne.ShortenUrl.Business;
using StripeOne.Core;
using StripeOne.Core.Log;

namespace StripeOne.ShortenUrl.Datalayer
{
    public class ShortenDatalayer : ShortenUrlDatalayerBase
    {
        public ShortenModel ShortIt(ShortenModel model)
        {
            try
            {
                using (var context = GetContext())
                {
                    model.Code = GenerateUniqueCode();
                    context.AddToShortenUrls(model.ToDatabase());
                    var rowsAffected = context.SaveChanges();

                    return model;
                }
            }
            catch (Exception ex)
            {
                StripeOneLog.Exception(ex);
                throw;
            }
        }

        private string GenerateUniqueCode()
        {
            try
            {
                var code = ShortenBusiness.GenerateUrlCode();
                using (var context = GetContext())
                {
                    var exists = (from urls in context.ShortenUrls
                                  where urls.ShortUrlCode.Equals(code)
                                  select urls).FirstOrDefault() != null;

                    if (exists)
                    {
                        code = GenerateUniqueCode();
                    }
                }
                return code;
            }
            catch (Exception ex)
            {
                StripeOneLog.Exception(ex);
                throw;
            }
        }

        public ShortenModel Get(int id)
        {
            try
            {
                using (var context = GetContext())
                {
                    var query = (from urls in context.ShortenUrls
                                 where urls.Id.Equals(id)
                                 select urls).FirstOrDefault();

                    var result = new ShortenModel(query);

                    return result;
                }
            }
            catch (Exception ex)
            {
                StripeOneLog.Exception(ex);
                throw;
            }
        }

        public ShortenModel[] GetAll(int userId)
        {
            try
            {
                using (var context = GetContext())
                {
                    var result = (from urls in context.ShortenUrls
                                  where urls.UserId.Value.Equals(userId)
                                  select urls).ToList().ToModel();

                    return result;
                }
            }
            catch (Exception ex)
            {
                StripeOneLog.Exception(ex);
                throw;
            }
        }
    }
}
